class_name MainMenuScreen
extends Screen

const MENU_TEXTURES := [
	[preload("res://assets/art/ui/mainmenu/1-player.png"),preload("res://assets/art/ui/mainmenu/1-player-selected.png")],
	[preload("res://assets/art/ui/mainmenu/2-players.png"),preload("res://assets/art/ui/mainmenu/2-players-selected.png")],
]

@onready var selectable_menu_node : Array[TextureRect] = [%SinglePlayerTexture,%TwoPlayerTexture]
@onready var selection_icon: TextureRect = %SelectionIcon

var current_selected_index := 0
var is_active = false

func _ready() -> void:
	refresh_ui()
	
func _process(delta: float) -> void:
	if is_active:
		if KeyUtil.is_action_just_pressed(Player.ControlScheme.P1, KeyUtil.Action.UP):
			change_select_index(current_selected_index - 1)
		elif KeyUtil.is_action_just_pressed(Player.ControlScheme.P1, KeyUtil.Action.DOWN):
			change_select_index(current_selected_index + 1)
		elif KeyUtil.is_action_just_pressed(Player.ControlScheme.P1, KeyUtil.Action.SHOOT):
			submit_selection()
		

func refresh_ui() -> void:
	for i in range(selectable_menu_node.size()):
		if i == current_selected_index:
			selectable_menu_node[i].texture = MENU_TEXTURES[i][1]
			selection_icon.position = selectable_menu_node[i].position + Vector2.LEFT * 25
		else:
			selectable_menu_node[i].texture = MENU_TEXTURES[i][0]
			#selection_icon.position = selectable_menu_node[i].position + Vector2.LEFT * 25

func change_select_index(new_index : int)-> void:
	current_selected_index = clamp(new_index,0, selectable_menu_node.size())
	SoundPlayer.play(SoundPlayer.Sound.UI_NAV)
	refresh_ui()

func submit_selection():
	SoundPlayer.play(SoundPlayer.Sound.UI_SELECT)
	var country_default:=DataLoader.get_countries()[1]
	var player_two = "" if current_selected_index == 0 else country_default
	GameManager.player_setup = [country_default, player_two]
	transition_screen(SoccerGame.ScreenType.TEAM_SELECTION)

func on_set_active() -> void:
	is_active = true
